	package textBuddyProcessor;
	
	import static org.junit.Assert.assertEquals;
	
	import java.io.File;
	import java.io.IOException;
	import java.util.Vector;
	
	import org.junit.Before;
	import org.junit.BeforeClass;
	import org.junit.Test;
	
	public class ControllerTesting {
	
		ControllerLogic testLogic = new ControllerLogic();
	
		@BeforeClass
		public static void setUpBeforeClass() throws Exception {
		}
	
		@Before
		public void setUp() throws Exception {
			// transfer contents from defFile.txt to a temp file.
		File f = new File("Temporary.txt");
		testLogic.setExistingFile(f);
		testLogic.setFileName("Temporary.txt");
	}
	
	@Test
	public void testGetValueOfCountIfFileExisted() {
		int expected = 0; // number of elements now.
		assertEquals(expected, testLogic.getValueOfCountIfFileExisted());
	}
	
	@Test
	public void testGetFileName() {
		String fileName = "Temporary.txt";
		assertEquals(fileName, testLogic.getFileName());
	}
	
	@Test
	public void testSetFileName() {
		testLogic.setFileName("aditi");
		assertEquals("aditi", testLogic.getFileName());
		testLogic.setFileName("Temporary.txt"); // to set it back after testing
												// is complete
	}
	
	@Test
	public void testAddContentToFile() throws IOException {
		testLogic.addContentToFile("aditi", true);
		testLogic.addContentToFile("Edward", true);
		testLogic.addContentToFile("aditi", true);
		testLogic.addContentToFile("Edward is mad", true);
		testLogic.addContentToFile("everyday is Edward", true);
		testLogic.addContentToFile("Edward has gone to the mall", true);
		int expected = 6;
		assertEquals(expected, ControllerLogic.getNumberOfElementsInFile());
		// also check by seraching.. implemented later
	}
	
	@Test
	public void testDeleteFromFile() throws IOException {
		testLogic.deleteFromFile("1");
		int expected = 5;
		assertEquals(expected, ControllerLogic.getNumberOfElementsInFile());
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void testDisplayContentsOfFile() throws IOException {
		String test[] = testLogic.displayContentsOfFile();
		String expected[] = { "1. Edward", "2. aditi", "3. Edward is mad",
				"4. everyday is Edward", "5. Edward has gone to the mall" };
		assertEquals(test, expected);
	}
	
	@Test
	public void testCheckIfEmpty() {
		boolean test = testLogic.checkIfEmpty();
		boolean expected = false;
		assertEquals(expected, test);
	}
	
	@Test
	public void testSearchIfElementExists() throws IOException {
		// TDD:
		Vector<String> testVector = new Vector<String>();
		String expected = "edward";
		testLogic.inputParamToSearch();
		assertEquals(expected, testLogic.userInput);
		// testLogic.searchIfElementExists();
		// Test 1: to search for multiple matches
		expected = "1. Edward";
		assertEquals(expected, (testLogic.sortedVector.elementAt(0)));
		expected = "3. Edward is mad";
		assertEquals(expected, (testLogic.sortedVector.elementAt(1)));
		expected = "4. everyday is Edward";
		assertEquals(expected, (testLogic.sortedVector.elementAt(2)));
		testLogic.clearFromFile(testLogic.getExistingFile());
		
		
		// Test 2: when the content does not exist:
		expected = "Sam";
		testLogic.inputParamToSearch();
		assertEquals(expected, testLogic.userInput);
		testVector.clear();
		assertEquals(testVector, testLogic.sortedVector);
		
		testLogic.addContentToFile("aditi", true);
		testLogic.addContentToFile("Edward", true);
		testLogic.addContentToFile("Edward is mad", true);
		testLogic.addContentToFile("everyday is Edward", true);
		testLogic.addContentToFile("Edward has gone to the mall", true);
		
		//single match
		expected = "Edward is mad";
		testLogic.inputParamToSearch();
		assertEquals(expected, testLogic.userInput);
		expected = "3. Edward is mad";
		assertEquals(expected, testLogic.sortedVector.elementAt(0));
		testLogic.clearFromFile(testLogic.getExistingFile());
		
	}
	
	@Test
	// JUNit Testing for sort:
	public void testwriteToFileFromVector() throws IOException {
		// JUNit Testing for sort:
		
		// Test 1: Has both uppercase and lowercase characters.
		Vector<String> testVector = new Vector<String>();
		testVector.add("aditi");
		testVector.add("Edward");
		testVector.add("Edward has gone to the mall");
		testVector.add("Edward is mad");
		testVector.add("everyday is Edward");
		testLogic.addContentToFile("aditi", true);
		testLogic.addContentToFile("Edward", true);
		testLogic.addContentToFile("Edward is mad", true);
		testLogic.addContentToFile("everyday is Edward", true);
		testLogic.addContentToFile("Edward has gone to the mall", true);
		testLogic.sortFileAlphabetically("A");
	    assertEquals(testVector, testLogic.sortedVector);
	    
	    //Test2: When the File is empty
	    testLogic.clearFromFile(testLogic.getExistingFile());
	    testVector.clear();
	    testLogic.sortFileAlphabetically("A");
	    assertEquals(testVector, testLogic.sortedVector);
	    
	    // Test 3: When the file has multiple repeats 
		testVector.add("aditi");
		testVector.add("Aditi");
		testVector.add("aditi is going");
		testVector.add("ankit");
		testVector.add("Ankit");
		testLogic.addContentToFile("aditi", true);
		testLogic.addContentToFile("ankit", true);
		testLogic.addContentToFile("aditi is going", true);
		testLogic.addContentToFile("Ankit", true);
		testLogic.addContentToFile("Aditi", true);
		testLogic.sortFileAlphabetically("A");
		assertEquals(testVector, testLogic.sortedVector);
		
		//Test 4: When the input has digits or special chars it follows the ASCII convention
		testLogic.clearFromFile(testLogic.getExistingFile());
		testVector.clear();
		testVector.add("08aditi1234");
		testVector.add("09aditi is going 78");
		testVector.add("09aditi4567");
		testVector.add("09ankit");
		testVector.add("Ankit78");
		testLogic.addContentToFile("08aditi1234", true);
		testLogic.addContentToFile("09ankit", true);
		testLogic.addContentToFile("09aditi is going 78", true);
		testLogic.addContentToFile("Ankit78", true);
		testLogic.addContentToFile("09aditi4567", true);
		testLogic.sortFileAlphabetically("A");
		assertEquals(testVector, testLogic.sortedVector);
		testLogic.clearFromFile(testLogic.getExistingFile());
		
		//Test 5: File has just one value
		testLogic.clearFromFile(testLogic.getExistingFile());
		testVector.clear();
		testVector.add("08aditi1234");
		testLogic.addContentToFile("08aditi1234", true);
		testLogic.sortFileAlphabetically("A");
		assertEquals(testVector, testLogic.sortedVector);
		testLogic.clearFromFile(testLogic.getExistingFile());

		}
	
	}
